package com.example.neusoft.sqlitedemo;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Neusoft on 2017/5/6.
 */
//RecyclerView的适配器
public class MyRVAdapter extends RecyclerView.Adapter<MyRVAdapter.ViewHolder> {
    Context context;
    List<ContactBean> list;

    MyRVAdapter(Context context, List<ContactBean>list){
        this.context=context;
        setList(list);
    }

    void setList(List<ContactBean> list){
        this.list=list;
    }
    public void clearList(){
        list.clear();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context)
                .inflate(R.layout.item_contact, parent,false);

        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final ContactBean contactBean=list.get(position);
        holder.name.setText(contactBean.getName());
        holder.tel.setText(contactBean.getTel());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, EditContactActivity.class);
                intent.putExtra("_id", contactBean.get_id());
                intent.putExtra("name", contactBean.getName());
                intent.putExtra("tel", contactBean.getTel());
                intent.putExtra("type", contactBean.getType());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list!=null)
            return list.size();
        else
            return 0;
    }

    public  static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView name;
        private TextView tel;

        public ViewHolder(View itemView) {
            super(itemView);
            name=(TextView)itemView.findViewById(R.id.textView_item_contact_name);
            tel=(TextView)itemView.findViewById(R.id.textView_item_contact_tel);
        }
    }
}
